<template>
  <div class="home">
    <div id="allmap"></div>
    <!-- 顶部搜索 -->
    <van-sticky :offset-top="0">
      <van-row>
        <van-col @click="tocity" span="4"
          ><p>{{ city }}</p></van-col
        >
        <van-col span="20"
          ><van-search
            @click="tosearch"
            v-model="value"
            placeholder="搜索商品"
            input-align="center"
        /></van-col>
      </van-row>
    </van-sticky>
    <!-- 轮播 -->
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="#666">
      <van-swipe-item v-for="item in data.banner" :key="item.id"
        ><img :src="item.image_url" alt=""
      /></van-swipe-item>
    </van-swipe>
    <!-- 轮播下的分类 -->
    <van-grid :border="false" :column-num="5">
      <van-grid-item
        @click="tocategorylist(value.name, value.id)"
        v-for="value in data.channel"
        :key="value.id"
        :icon="value.icon_url"
        :text="value.name"
      />
    </van-grid>
    <!-- 品牌制造商直供 -->
    <!-- <div class="topicList"> -->

    <div class="brandList">
      <div @click="tobrandlist" class="name">
        <span>品牌制造商直供</span>
        <span class="icon"><van-icon name="arrow" /></span>
      </div>
      <div
        @click="tobranddetail(item.id)"
        class="item"
        v-for="item in data.brandList"
        :key="item.id"
      >
        <img :src="item.pic_url" alt="" />
        <h4>{{ item.name }}</h4>
        <p>{{ item.floor_price }}</p>
      </div>
    </div>
    <!-- 新品首发 -->
    <div class="newGoods">
      <div @click="tonewhotgoods('isNew')" class="newGoods-top">
        <h3>新品首发</h3>
        <p>查看全部</p>
      </div>
      <div class="goods">
        <div
          class="item"
          @click="togoodsinfo(item.id)"
          :class="index == 0 ? '' : 'act'"
          v-for="(item, index) in data.newGoods"
          :key="item.id"
        >
          <img :src="item.list_pic_url" alt="" />
          <h3>{{ item.name }}</h3>
          <h4>{{ item.goods_brief }}</h4>
          <p>¥{{ item.retail_price }}</p>
        </div>
      </div>
    </div>
    <!-- 人气推荐好物精选 -->
    <div class="newGoods hotGoods">
      <div @click="tonewhotgoods('isHot')" class="newGoods-top hotGoods-top">
        <h3>人气推荐.精选好物</h3>
        <p>查看全部</p>
      </div>
      <div class="goods">
        <div
          class="item"
          @click="togoodsinfo(item.id)"
          :class="index == 0 ? '' : 'act'"
          v-for="(item, index) in data.hotGoods"
          :key="item.id"
        >
          <img :src="item.list_pic_url" alt="" />
          <h3>{{ item.name }}</h3>
          <h4>{{ item.goods_brief }}</h4>
          <p>¥{{ item.retail_price }}</p>
        </div>
      </div>
    </div>
    <!-- 专题推荐 -->
    <div class="topicList">
      <p @click="totopic" class="title">
        <span>专题推荐</span>
        <span class="icon"><van-icon name="arrow" /></span>
      </p>
      <div class="topic">
        <div
          class="item"
          @click="totopicdetail(item.id)"
          v-for="item in data.topicList"
          :key="item.id"
        >
          <img :src="item.item_pic_url" alt="" />
          <div class="item-con">
            <h3>{{ item.title }}</h3>
            <h4>{{ item.price_info }}元/起</h4>
          </div>
          <p>{{ item.subtitle }}</p>
        </div>
      </div>
    </div>
    <!-- 下方分类内容 -->
    <div class="newCategoryList">
      <div class="item" v-for="item in data.newCategoryList" :key="item.id">
        <h3>{{ item.name }}好物</h3>
        <div class="item-con">
          <div
            class="con-list"
            @click="togoodsinfo(con.id)"
            v-for="con in item.goodsList"
            :key="con.id"
          >
            <img :src="con.list_pic_url" alt="" />
            <h3>{{ con.name }}</h3>
            <p>¥{{ con.retail_price }}</p>
          </div>
          <div
            @click="tocategorylist(item.name, item.id)"
            class="con-list con-last"
          >
            <h3>{{ item.name }}好物</h3>
            <div class="icon">
              <van-icon name="down" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { index, goodsList, detailaction, listaction } from "@/api/home/index";
export default {
  components: {},

  data() {
    return {
      data: {},
      value: "",
      city: "",
      cityshow: "",
    };
  },
  methods: {
    tosearch() {
      this.$router.push({ name: "search" });
    },
    //到分类详细页面
    tocategorylist(value, sortid) {
      console.log(value, sortid);
      this.$router.push({
        name: "categorylist",
        params: {
          type: value,
          id: sortid,
        },
      });
    },
    //到商品详细页面
    togoodsinfo(goodsid) {
      console.log(goodsid);
      this.$router.push({
        name: "goodsinfo",
        params: {
          id: goodsid,
        },
      });
    },
    // 到专题页面
    totopic() {
      this.$router.push({ name: "topic" });
    },

    // 到专题详细
    totopicdetail(num) {
      this.$router.push({
        name: "topicdetail",
        params: {
          id: num,
        },
      });
    },
    // 到热门和最新产品页面
    tonewhotgoods(type) {
      this.$router.push({
        name: "newhotgoods",
        params: {
          type: type,
        },
      });
    },
    // 到品牌列表页
    tobrandlist() {
      this.$router.push({ name: "brandlist" });
    },
    //到品牌直供详细页面
    tobranddetail(id) {
      this.$router.push({
        name: "branddetail",
        params: {
          id: id,
        },
      });
    },
    tocity() {
      this.$router.push({ name: "city" });
    },
  },
  created() {
    goodsList({ isNew: 1 }).then((res) => {
      // console.log(res);
    });
    index().then((res) => {
      console.log(res);
      this.data = res;
    });
    if (this.$route.params.local) {
      localStorage.setItem("city", this.$route.params.local);
    }
  },
  mounted() {
    var _this = this;
    var map = new BMap.Map("allmap");
    var point = new BMap.Point(116.331398, 39.897445);
    map.centerAndZoom(point, 12);

    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(
      function (r) {
        // console.log(r);
        var local = localStorage.getItem("city");
        if (local) {
          _this.city = local;
        } else {
          var result = r.address.city;
          _this.city = result;
        }
      },
      { enableHighAccuracy: true }
    );
  },
  computed: {},
};
</script>

<style scoped lang='less'>
// 顶部搜索
.van-row {
  background-color: #fff;
  p {
    width: 70px;
    height: 53px;
    line-height: 53px;
    margin-left: 10px;
    font-size: 12px;
  }
}
.van-swipe {
  width: 375px;
  img {
    width: 375px;
  }
  .van-swipe__indicator {
    background-color: #999;
  }
}
.bottm {
  width: 375px;
  height: 200px;
}

// 品牌制造商直供
.brandList {
  width: 365px;
  background-color: #fff;
  padding: 5px 5px;
  margin: 10px auto;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  h3 {
    width: 365px;
    margin: 10px auto;
    font-size: 16px;
    font-weight: 400;
    .icon {
      padding-top: 20px;
      i {
        font-size: 18px;
        transform: rotate(90deg);
        line-height: 23px;
      }
    }
  }

  .item {
    width: 180px;
    height: 112px;
    margin-top: 5px;
    position: relative;
    h4 {
      position: absolute;
      z-index: 2;
      left: 5px;
      top: 20px;
    }
    p {
      position: absolute;
      z-index: 2;
      left: 5px;
      top: 50px;
    }
    img {
      width: 180px;
      height: 112px;
      position: absolute;
      left: 0;
      top: 0;
      z-index: 1;
    }
  }
  .name {
    width: 375px;
    height: 50px;
    padding: 0 10px;
    span {
      line-height: 50px;
    }
    .icon {
      width: 16px;
      height: 16px;
      border-radius: 8px;
      position: relative;
      i {
        position: absolute;
        left: 4px;
        top: 1px;
      }
    }
  }
}

// 新品首发
.newGoods {
  width: 375px;
  margin: 10px auto;
  .newGoods-top {
    width: 375px;
    height: 130px;
    background-image: url(../../assets/newgoodstop.png);
    overflow: hidden;
    h3 {
      margin-top: 30px;
      color: #8c9bae;
      font-size: 16px;
      font-weight: 400;
    }
    p {
      width: 90px;
      height: 25px;
      line-height: 25px;
      background-color: #d8e4f0;
      color: #8c9bae;
      margin: 10px auto;
    }
  }
  .goods {
    background-color: #fff;
    margin-top: 10px;
    padding: 10px 0;
    width: 375px;
    display: flex;
    justify-content: space-between;
    text-align: left;
    overflow: auto;
    height: 210px;
    .act::after {
      content: "";
      display: block;
      height: 180px;
      width: 1px;
      background-color: #eee;
      position: relative;
      left: -10px;
      top: -200px;
    }
    .item {
      width: 140px;
      margin-left: 10px;
      img {
        width: 140px;
      }
      h3 {
        width: 110px;
        color: #666;
        margin: 5px 0;
        margin-left: 10px;
      }
      h4 {
        width: 110px;
        color: #999;
        margin: 5px 0;
        margin-left: 10px;
      }
      p {
        color: red;
        margin-left: 10px;
      }
    }
  }
}

// 人气好物推荐
.hotGoods {
  .hotGoods-top {
    background-image: url(../../assets/hotGoods.png);
    h3 {
      color: #b1a279;
    }
    p {
      color: #b1a279;
      background-color: #f4e9cb;
    }
  }
}
// 专题推荐
.topicList {
  overflow: auto;
  width: 375px;
  background-color: #fff;
  margin: 10px auto;
  .title {
    padding: 20px 0;
    line-height: 22px;
    width: 90px;
    margin: 0 auto;
    span {
      display: block;
      float: left;
      margin: 0 5px;
      line-height: 20px;
    }
    .icon {
      border: 1px solid #999;
      width: 16px;
      height: 16px;
      line-height: 21px;
      border-radius: 8px;
    }
  }
  .topic {
    width: 375px;
    overflow: auto;
    display: flex;
    justify-content: space-between;
    padding: 0 0px 20px 0;
    .item {
      width: 287px;
      margin-left: 10px;
      img {
        width: 287px;
        height: 162px;
        border-radius: 8px;
      }
      .item-con {
        padding: 10px 0;
        width: 287px;
        display: flex;
        justify-content: space-between;
        h4 {
          color: red;
        }
      }
      p {
        text-align: left;
        color: #999;
        width: 250px;
      }
    }
  }
}

// 下方分类内容
.newCategoryList {
  width: 365px;
  padding: 10px 5px;
  margin-bottom: 50px;
  .item {
    width: 365px;
    h3 {
      width: 365px;
      margin: 20px 0;
    }
    .item-con {
      width: 365px;
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      .con-list {
        width: 180px;
        height: 216px;
        background-color: #fff;
        margin-top: 5px;
        img {
          width: 151px;
          height: 151px;
        }
        h3 {
          width: 160px;
          margin: 10px auto;
          text-align: left;
        }
        p {
          width: 160px;
          margin: 10px auto;
          text-align: left;
          color: red;
        }
      }
      .con-last {
        h3 {
          width: 180px;
          text-align: center;
          color: #666;
          font-size: 18px;
          margin-top: 60px;
        }
        .icon {
          width: 35px;
          height: 35px;
          text-align: center;
          line-height: 39px;
          color: #999;
          font-size: 20px;
          border: 2px solid #999;
          border-radius: 22px;
          transform: rotate(-90deg);
          margin: 20px auto;
        }
      }
    }
  }
}
</style>